package so.shanku.essential.activity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.umeng.analytics.MobclickAgent;

import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;

/**
 * 
 * 我的账号
 * @author FangDongzhang
 * 2016/4/15
 *
 */
@SuppressWarnings({ "unused", "deprecation" })
public class MyAccountActivity extends BaseUIActivity{

	@ViewInject(id = R.id.left,click="left")LinearLayout left;
	@ViewInject(id = R.id.img1)ImageView mImageView;
	@ViewInject(id = R.id.title_lay)LinearLayout titleLayout;
	@ViewInject(id = R.id.lay)LinearLayout layout;
	@ViewInject(id = R.id.img1)TextView textView;
	
	private float mCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离
	private HorizontalScrollView mHorizontalScrollView;//上面的水平滚动控件
	private ViewPager mViewPager;	//下方的可横向拖动的控件
	private ArrayList<View> mViews;//用来存放下方滚动的layout(layout_1,layout_2,layout_3)
	LocalActivityManager manager = null;
	private RadioGroup myRadioGroup;
	private int _id = 0;
	private int width;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_account);
		
		setCenter_title(R.string.myAccount);
		setBtn_menu(R.drawable.more_white, null);
		
		WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(dm);	// 屏幕宽度
		width = dm.widthPixels;
//
		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(savedInstanceState);
		getTitleInfo();
		initGroup();
		iniListener();
		iniVariable();
		
	}
	
	private List<Map<String, Object>> titleList = new ArrayList<Map<String,Object>>();
	private void getTitleInfo(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", 0);
		map.put("title", "我的余额");
		titleList.add(map);
		map = new HashMap<String, Object>();
		map.put("id", 1);
		map.put("title", "我的优惠券");
		titleList.add(map);
	}

	private void initGroup(){
		mHorizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);
		mViewPager = (ViewPager)findViewById(R.id.pager);
		myRadioGroup = new RadioGroup(this);
		myRadioGroup.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		myRadioGroup.setOrientation(LinearLayout.HORIZONTAL);
		layout.addView(myRadioGroup);
		for (int i = 0; i <titleList.size(); i++) {
			Map<String, Object> map = titleList.get(i);
			RadioButton radio = new RadioButton(this);

			radio.setButtonDrawable(android.R.color.transparent); 
			LinearLayout.LayoutParams l = new LinearLayout.LayoutParams(width/2, LayoutParams.MATCH_PARENT, Gravity.CENTER);
			radio.setLayoutParams(l);
			radio.setGravity(Gravity.CENTER);
			radio.setPadding(20, 15, 20, 15);
			radio.setId(_id+i);
			radio.setText(map.get("title")+"");
			radio.setTextColor(getResources().getColor(R.color.text_color_gray));
			radio.setTag(map);
			if (i == 0) {				
				radio.setChecked(true);
				radio.setTextColor(getResources().getColor(R.color.title_color));
				int itemWidth = (int) radio.getPaint().measureText(map.get("title")+"");
				mImageView.setLayoutParams(new  LinearLayout.LayoutParams(width/2,2));
			}
			myRadioGroup.addView(radio);
		}
		myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@SuppressWarnings({ "unchecked" })
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int radioButtonId = group.getCheckedRadioButtonId(); 
				//根据ID获取RadioButton的实例 
				RadioButton rb = (RadioButton)findViewById(radioButtonId);
				Map<String, Object> selectMap = (Map<String, Object>) rb.getTag();

				AnimationSet animationSet = new AnimationSet(true);
				TranslateAnimation translateAnimation;
				translateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, rb.getLeft(), 0f, 0f);
				animationSet.addAnimation(translateAnimation);
				animationSet.setFillBefore(true);
				animationSet.setFillAfter(true);
				animationSet.setDuration(300);

				for (int i = 0; i < titleList.size(); i++) {
					RadioButton rb1 = (RadioButton)findViewById(i);
					rb1.setTextColor(getResources().getColor(R.color.text_color_gray));
				}
				rb.setTextColor(getResources().getColor(R.color.title_color));

				mImageView.startAnimation(animationSet);//开始上面蓝色横条图片的动画切换
				mViewPager.setCurrentItem(radioButtonId-_id);//让下方ViewPager跟随上面的HorizontalScrollView切换
				mCurrentCheckedRadioLeft = rb.getLeft();//更新当前蓝色横条距离左边的距离
				mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)getResources().getDimension(R.dimen.findproduct_hv_item_width), 0);

				mImageView.setLayoutParams(new  LinearLayout.LayoutParams(rb.getRight()-rb.getLeft(),4));
			}
		});
	}

	private View getView(String id, Intent intent) {
		return manager.startActivity(id, intent).getDecorView();
	}

	private void iniVariable() {
		mViews = new ArrayList<View>();
		Intent intent0 = new Intent(this,MyBalanceActivity.class);
//		intent0.putExtra("code",XlID);
		mViews.add(getView("View"+0, intent0));
		
		Intent intent1 = new Intent(this,MyCouponActivity.class);
//		intent1.putExtra("code",XlID);
		mViews.add(getView("View"+0, intent1));
		
		mViewPager.setAdapter(new MyPagerAdapter());//设置ViewPager的适配器
	}




	private void iniListener() {
		mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());
	}
	/**
	 * ViewPager的适配器
	 */
	private class MyPagerAdapter extends PagerAdapter{

		@Override
		public void destroyItem(View v, int position, Object obj) {
			((ViewPager)v).removeView(mViews.get(position));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mViews.size();
		}

		@Override
		public Object instantiateItem(View v, int position) {
			((ViewPager)v).addView(mViews.get(position));
			return mViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

	}
	/**
	 * ViewPager的PageChangeListener(页面改变的监听器)
	 */
	private class MyPagerOnPageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
		/**
		 * 滑动ViewPager的时候,让上方的HorizontalScrollView自动切换
		 */
		@Override
		public void onPageSelected(int position) {
			System.out.println("====="+position);
			RadioButton radioButton = (RadioButton) findViewById(_id+position);
			radioButton.performClick();
			
			switch (position) {
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			default:
				break;
			}
		}
	}
	
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
